<?php
include('../pages/config.php');
session_start();
###---  This file is used for updating subscriber features

$current_user = $_SESSION['myusername'];
$directoryNumber = $_GET['queryString'];
$debug = $_GET['debug'];
$cmd = $_GET['cmd'];
$pin = $_GET['PIN'];
$longDistanceCarrier = $_GET['longDistanceCarrier'];
$intraLATACarrier = $_GET['intraLATACarrier'];
$internationalCarrier = $_GET['internationalCarrier'];
$serviceStatus = $_GET['serviceStatus'];
$accessDeviceName = $_GET['accessDeviceName'];
$gatewayName = $_GET['gatewayName'];
$accessLineNumber = $_GET['accessLineNumber'];
$callAgentSignalingType = $_GET['callAgentSignalingType'];

$client = new SoapClient('sdk/xml/soap/ShServiceTyped.wsdl');

//  This section creates the parameters to pass to the Meta for a Pull (Query)
$params = array(
		'UserIdentity' => $directoryNumber,
		'DataReference' => 0,
		'ServiceIndication' => 'Meta_Subscriber_BaseInformation',
		'OriginHost' => 'amherst.com?clientVersion=7.0'
		);

//  Send the Pull (Query) to the Meta and put the returned results into $resultCode
$pullResultCode = $client->ShPull($params);

if ($debug == "True") {
	echo "<pre>";
	print_r($pullResultCode);
	echo "</pre>";
}

//  If the result returns a failure, echo the result detail to the screen and stop
if ($pullResultCode->{'ResultCode'} == "5001") {
	echo $pullResultCode->{'ExtendedResult'}->{'ExtendedResultDetail'};
}
else {
//  $Meta_Sub_Base is put in place, so we don't have to keep repeating this for each of the results below
$Meta_Sub_Base = $pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'};

//  The sequence number is a random number returned by the Meta so that multiple people can't make concurrent changes to a customer
//  We later need to increase the number by 1 for the update
$sequenceNumber = $pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'SequenceNumber'};

if ($cmd == "meta_update_sub") {
	//  Below updates the values in the returned results according to the changes done	
	//  Sequence Number needs to be increased by 1 when it is returned.  This is per Meta's documentation
	$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'SequenceNumber'} = $sequenceNumber + 1;

	//  Below we will figure out what changes are being done so we can log them in the database
	//  This section needs to be before we make the changes to the results!
	$meta_changes_log = NULL;
	if ($Meta_Sub_Base->{'PIN'} != $pin) {
		//PIN has been changed
		$meta_changes_log['PIN'] = $pin;
	}
	if ($Meta_Sub_Base->{'ServiceSuspended'} != $serviceStatus) {
		//Service state has been changed
		$meta_changes_log['ServiceStatus'] = $serviceStatus;
	}	
	if ($Meta_Sub_Base->{'LongDistanceCarrier'}->{'Value'} != $longDistanceCarrier) {
		//Long distance carrier has been changed
		$meta_changes_log['LongDistanceCarrier'] = $longDistanceCarrier;
	}
	if ($Meta_Sub_Base->{'IntraLATACarrier'}->{'Value'} != $intraLATACarrier) {
		//Intra LATA carrier has been changed
		$meta_changes_log['IntraLATACarrier'] = $intraLATACarrier;
	}
	if ($Meta_Sub_Base->{'InternationalCarrier'}->{'Value'} != $internationalCarrier) {
		//International carrier has been changed
		$meta_changes_log['InternationalCarrier'] = $internationalCarrier;
	}
	if ($callAgentSignalingType == 'Analog Line/T1 CAS') {
		if ($Meta_Sub_Base->{'AccessDevice'}->{'AccessDeviceName'} != $accessDeviceName) {
			//Access device has been changes
			$meta_changes_log['AccessDeviceName'] = $accessDeviceName;
		}
		if ($Meta_Sub_Base->{'AccessLineNumber'} != $accessLineNumber) {
			//Access device has been changes
			$meta_changes_log['AccessLineNumber'] = $accessLineNumber;
		}
	}
	
	//  Below are each of the items in the returned Results that we possibly would want to change.
	//  We will "Update" the entire subscriber, even if only 1 change is made.  This is done so that multiple
	//  changes can be made and submitted.  May look at revising this in the future.
	$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'PIN'} = $pin;
	$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'ServiceSuspended'} = $serviceStatus;
	$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'LongDistanceCarrier'}->{'Value'} = $longDistanceCarrier;
	$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'IntraLATACarrier'}->{'Value'} = $intraLATACarrier;
	$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'InternationalCarrier'}->{'Value'} = $internationalCarrier;
	if ($callAgentSignalingType == 'Analog Line/T1 CAS') {
		$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'AccessDevice'}->{'GatewayName'} = $gatewayName;
		$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'AccessDevice'}->{'AccessDeviceName'} = $accessDeviceName;
		$pullResultCode->{'UserData'}->{'Sh-Data'}->{'RepositoryData'}->{'ServiceData'}->{'MetaSwitchData'}->{'Meta_Subscriber_BaseInformation'}->{'AccessLineNumber'} = $accessLineNumber;
	}

	// This section creates the parameters to pass to the Meta for an Update
	$update_params = array(
				'UserIdentity' => $directoryNumber,
				'DataReference' => 0,
				//  $resultCode->{'UserData'} needs to be passed, because Meta wants to see the Sh-Data first
				'UserData' => $pullResultCode->{'UserData'},
				'OriginHost' => 'amherst.com?clientVersion=7.0'
				);

	//  Send the update and put the returned results into $updateResults
	$updateResults = $client->ShUpdate($update_params);	
	
	//  If the returned result code is not a 2001 (Success), then output the error returned
	if ($updateResults->{'ResultCode'} != 2001) {
		echo "<br/><br/><b>" . $updateResults->{'ExtendedResult'}->{'ExtendedResultDetail'} . "</b>";
	}
	else {
		echo "<br/><br/><b>Success!!</b><br/>";
		//  Since the result came back successful, add a log to the database if there were changes made
		//  If a form is just submitted without changes, then don't log anything because no changes were made
		if (!empty($meta_changes_log)) {
			$meta_changes_csv = NULL;
			// Parse out the array into a CSV format to be put in the database for records
			while ($item = current($meta_changes_log)) {
				$meta_changes_csv .= key($meta_changes_log) . "=" . $item . ",\n";
				next($meta_changes_log);
			}
			mysql_query("INSERT INTO metaswitch_log (trans_id, trans_dn, trans_changes, submitted_by) VALUES (NULL, '$directoryNumber', '$meta_changes_csv', '$current_user')");
		}
	}

	if ($debug == "True") {
		echo "<pre>";
		print_r($updateResults);
		echo "</pre>";
	}
}
}

?>

